# -*- coding: UTF-8 -*-

import pandas as pd
import traceback

from util.TimeUtils import TimeUtils
import logging

from Predictor import PredictorControl

desired_width = 320
pd.set_option('display.width', desired_width)
pd.set_option('display.max_columns', 20)


class TrainParameter:
    def __init__(self, predict_type, window, sequence_length, begin_utc_time, end_utc_time):
        self.predict_type = predict_type
        self.window = window
        self.sequence_length = sequence_length
        self.begin_utc_time = begin_utc_time
        self.end_utc_time = end_utc_time


def test_hist_predict_loop(predict_type, window, sequence_length, begin_utc_time, end_utc_time):
    try:
        # modelpath = './saved_models/test_20190101/%s-%d-%d-20190101.xgb' % (
        #     predict_type, window, sequence_length)
        # modelpath = './saved_models_2019/%s-%d-%d-旧的特征文件_20181201分割训练集.xgb' % (
        #     predict_type, window, sequence_length)
        modelpath = './saved_models_2019/0225/%s-%d-%d-20190225.xgb' % (predict_type, window, sequence_length)
        predictor = PredictorControl(ip='39.104.227.148', xgboost_model_path=modelpath, window=window,
                                     predict_type=predict_type, sequence_length=sequence_length)
        pre_timestamp = TimeUtils.utc2timestamp(begin_utc_time)
        end_timestamp = TimeUtils.utc2timestamp(end_utc_time)
        while pre_timestamp <= end_timestamp:
            predictor.product_signal(pre_timestamp, test=False, store_db=True, post_signal=False)
            pre_timestamp += (window * 60)
    except Exception as e:
        print('error: %s' % e)
        print('predict task failed, please check!!!')
        traceback.print_exc()


def generate_hist_signal():
    #     begin_utc_time = '2019-03-05 00:00:00'
    #     end_utc_time = '2019-03-05 01:00:00'
    #     begin_utc_time = '2019-02-15 05:00:00'
    #     end_utc_time = '2019-02-15 05:00:00'
    begin_utc_time = '2019-03-05 00:00:00'
    end_utc_time = '2019-03-12 07:00:00'
    return_15_72 = TrainParameter(predict_type='return', window=15, sequence_length=72, begin_utc_time=begin_utc_time,
                                  end_utc_time=end_utc_time)
    return_30_72 = TrainParameter(predict_type='return', window=30, sequence_length=72, begin_utc_time=begin_utc_time,
                                  end_utc_time=end_utc_time)
    return_60_72 = TrainParameter(predict_type='return', window=60, sequence_length=72, begin_utc_time=begin_utc_time,
                                  end_utc_time=end_utc_time)
    return_15_132 = TrainParameter(predict_type='return', window=15, sequence_length=132, begin_utc_time=begin_utc_time,
                                   end_utc_time=end_utc_time)
    return_30_132 = TrainParameter(predict_type='return', window=30, sequence_length=132, begin_utc_time=begin_utc_time,
                                   end_utc_time=end_utc_time)
    volatility_15_72 = TrainParameter(predict_type='volatility', window=15, sequence_length=72,
                                      begin_utc_time=begin_utc_time,
                                      end_utc_time=end_utc_time)
    volatility_30_72 = TrainParameter(predict_type='volatility', window=30, sequence_length=72,
                                      begin_utc_time=begin_utc_time,
                                      end_utc_time=end_utc_time)
    param_list = [return_15_72, return_30_72, return_60_72, return_15_132, return_30_132, volatility_15_72,
                  volatility_30_72]
    # param_list = [return_15_72]
    for param in param_list:
        test_hist_predict_loop(param.predict_type, param.window, param.sequence_length,
                               param.begin_utc_time, param.end_utc_time)


if __name__ == '__main__':
    LOG_FORMAT = '%(asctime)s - %(levelname)s - %(message)s'
    DATE_FORMAT = '%Y-%m-%d %H:%M:%S'
    # log_file_path = 'xx.log'
    logging.basicConfig(level=logging.INFO, format=LOG_FORMAT, datefmt=DATE_FORMAT)
    generate_hist_signal()
